/*=========================================================================
<<<<<<< Field.cc
	UberSim Source Code Release
	-------------------------------------------------------------------------
	Copyright (C) 2002 Manuela Veloso, Brett Browning, Mike Bowling,
	James Bruce; {mmv, brettb, mhb, jbruce}@cs.cmu.edu
	Erick Tryzelaar {erickt}@andrew.cmu.edu
	School of Computer Science, Carnegie Mellon University
	-------------------------------------------------------------------------
	This software is distributed under the GNU General Public License,
	version 2.  If you do not have a copy of this licence, visit
	www.gnu.org, or write: Free Software Foundation, 59 Temple Place,
	Suite 330 Boston, MA 02111-1307 USA.  This program is distributed
	in the hope that it will be useful, but WITHOUT ANY WARRANTY,
	including MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
	-------------------------------------------------------------------------*/

#ifdef USE_OPENGL
#include <drawstuff/drawstuff.h>
#endif

#include "../utils/configreader.h"

#include "mmgr.h"

#include "BoxGeometry.h"
#include "Field.h"
#include "Geometry.h"
#include "PlaneGeometry.h"


CR_DECLARE(WALL_THICKNESS);
CR_DECLARE(WALL_HEIGHT);
CR_DECLARE(FIELD_WALL_HEIGHT);
CR_DECLARE(FIELD_WALL_SEP);

CR_DECLARE(FIELD_WIDTH);
CR_DECLARE(FIELD_LENGTH);
CR_DECLARE(GOAL_DEPTH);
CR_DECLARE(GOAL_WIDTH);


static const double scale = 10.0; // m -> dm

Field::Field (char* name) : Node (name)
{
  static bool isConfiged = false;

  if (!isConfiged) {
    isConfiged = true;

    CR_SETUP(Field, WALL_THICKNESS, CR_DOUBLE);
    CR_SETUP(Field, WALL_HEIGHT, CR_DOUBLE);
    CR_SETUP(Field, FIELD_WALL_HEIGHT, CR_DOUBLE);
    CR_SETUP(Field, FIELD_WALL_SEP, CR_DOUBLE);
    
    CR_SETUP(Field, FIELD_WIDTH, CR_DOUBLE);
    CR_SETUP(Field, FIELD_LENGTH, CR_DOUBLE);
    CR_SETUP(Field, GOAL_WIDTH, CR_DOUBLE);
    CR_SETUP(Field, GOAL_DEPTH, CR_DOUBLE);
  }

  PlaneGeometry* plane = new PlaneGeometry(Vector3::UNIT_Z, 0, "Ground");	
  addChild (plane);

  Geometry *geom;

  Vector3 pos;
  Real size[3];

  for (int side = -1; side <= 1; side += 2) {
    // goal side
    for (int j = -1; j <= 1; j += 2) {
      geom = new BoxGeometry(DVAR(GOAL_DEPTH) * scale,
                             DVAR(WALL_THICKNESS) * scale,
                             DVAR(WALL_HEIGHT) * scale,
                             "Yellow Goal left side");
      addChild(geom);
      pos.set(side * (DVAR(FIELD_LENGTH) + DVAR(GOAL_DEPTH))/2*scale,
              j * (DVAR(GOAL_WIDTH) + DVAR(WALL_THICKNESS))/2*scale,
              0);
      geom->setLocalPosition(pos);
    }
    
    // goal back
    geom = new BoxGeometry(DVAR(WALL_THICKNESS) * scale,
                           DVAR(GOAL_WIDTH) * scale,
                           DVAR(WALL_HEIGHT) * scale,
                           "Yellow Goal left side");
    addChild(geom);
    pos.set(side * (DVAR(FIELD_LENGTH)/2 + DVAR(GOAL_DEPTH))*scale,
            0, 0);
    geom->setLocalPosition(pos);

    // side outer wall
    geom = new BoxGeometry((DVAR(FIELD_LENGTH) + DVAR(FIELD_WALL_SEP)*2)*scale,
                           DVAR(WALL_THICKNESS) * scale,
                           DVAR(FIELD_WALL_HEIGHT) * scale,
                           "field wall");
    addChild(geom);
    pos.set(0, side*(DVAR(FIELD_WIDTH)/2 + DVAR(FIELD_WALL_SEP))*scale, 0);
    geom->setLocalPosition(pos);

    // back outer wall
    geom = new BoxGeometry(DVAR(WALL_THICKNESS) * scale,
                           (DVAR(FIELD_WIDTH) + DVAR(FIELD_WALL_SEP)*2)*scale,                           
                           DVAR(FIELD_WALL_HEIGHT) * scale,
                           "field wall");
    addChild(geom);
    pos.set(side*(DVAR(FIELD_LENGTH)/2 + DVAR(FIELD_WALL_SEP))*scale, 0, 0);
    geom->setLocalPosition(pos);
  }




#if 0
  geom = new BoxGeometry(size, "Yellow Goal Right Side");
  addChild(geom);
  pos.set(VDVAR(GOAL_SIDE_POSITION)[0]*scale,
          -VDVAR(GOAL_SIDE_POSITION)[1]*scale,
          VDVAR(GOAL_SIDE_POSITION)[2]*scale);
  geom->setLocalPosition(pos);

  geom = new BoxGeometry(size, "Blue Goal Left Side");
  addChild(geom);
  pos.set(-VDVAR(GOAL_SIDE_POSITION)[0]*scale,
          VDVAR(GOAL_SIDE_POSITION)[1]*scale,
          VDVAR(GOAL_SIDE_POSITION)[2]*scale);
  geom->setLocalPosition(pos);

  geom = new BoxGeometry(size, "Blue Goal Right Side");
  addChild(geom);
  pos.set(-VDVAR(GOAL_SIDE_POSITION)[0]*scale,
          -VDVAR(GOAL_SIDE_POSITION)[1]*scale,
          VDVAR(GOAL_SIDE_POSITION)[2]*scale);
  geom->setLocalPosition(pos);

  for (int i = 0; i < 3; i++)
    size[i] = VDVAR(GOAL_BACK_SIZE)[i] * scale;
	
  geom  = new BoxGeometry(size, "Yellow Goal Back");
  addChild(geom);
  geom->setLocalPosition(Vector3(VDVAR(GOAL_BACK_POSITION)) * scale);

  geom  = new BoxGeometry(size, "Blue Goal Back");
  addChild(geom);
  geom->setLocalPosition(-Vector3(VDVAR(GOAL_BACK_POSITION)) * scale);

  // field wall
  size[0] = VDVAR(FIELD_WALL_SIZE)[0] * scale;
  size[1] = VDVAR(FIELD_WALL_SIZE)[3] * scale;
  size[2] = VDVAR(FIELD_WALL_SIZE)[2] * scale;
  geom  = new BoxGeometry(size, "side wall 1");
  addChild(geom);
  geom->setLocalPosition(Vector3(0, VDVAR(FIELD_WALL_SIZE)[1], 0) * scale);
  geom  = new BoxGeometry(size, "side wall 2");
  addChild(geom);
  geom->setLocalPosition(Vector3(0, -VDVAR(FIELD_WALL_SIZE)[1], 0) * scale);

  size[0] = VDVAR(FIELD_WALL_SIZE)[3] * scale;
  size[1] = VDVAR(FIELD_WALL_SIZE)[1] * scale;
  size[2] = VDVAR(FIELD_WALL_SIZE)[2] * scale;
  geom  = new BoxGeometry(size, "back wall 1");
  addChild(geom);
  geom->setLocalPosition(Vector3(VDVAR(FIELD_WALL_SIZE)[0], 0, 0) * scale);
  geom  = new BoxGeometry(size, "side wall 2");
  addChild(geom);
  geom->setLocalPosition(Vector3(-VDVAR(FIELD_WALL_SIZE)[0], 0, 0) * scale);
#endif
}

Field::~Field ()
{
}
